package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private FlavorMapper flavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.save(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
        }
        flavorMapper.save(flavors);
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
        ids.forEach(id->{
            Dish dish=dishMapper.byId(id);
            if (dish.getStatus()==1){
                throw new DeletionNotAllowedException("菜品正在售卖中，不能删除");
            }
        });
        List<Long> list=setmealMapper.byId(ids);
        if (list.size()>0 && list != null){
            throw new DeletionNotAllowedException("菜品关联套餐，不能删除");
        }
        dishMapper.delete(ids);
        ids.forEach(id->{
            flavorMapper.delete(id);
        });
    }

    @Override
    public DishVO byId(Long id) {
        Dish dish = dishMapper.byId(id);
        List<DishFlavor> dishFlavors =flavorMapper.byId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        flavorMapper.delete(dish.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            flavorMapper.save(flavors);
        }
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder().status(status).id(id).build();
        dishMapper.update(dish);
        if (status == 0) {
            List<Long> dishIds=new ArrayList<>();
            dishIds.add(id);
            List<Long> list = setmealDishMapper.getSetmealIdByDishIds(dishIds);
            if (list != null && list.size() > 0) {
                for (Long l : list) {
                    Setmeal setmeal = Setmeal.builder().id(l).status(0).build();
                    setmealMapper.update(setmeal);
                }
            }
        }
    }

    @Override
    public List<Dish> list(Long categoryId) {
        List<Dish> list = dishMapper.list(categoryId);
        return list;
    }

    @Override
    public List<DishVO> byCategoryId(Dish dish) {
        List<Dish> list = dishMapper.byCategoryId(dish);
        if (list != null && list.size() > 0) {
            List<DishVO> dishVOS = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                DishVO dishVO = new DishVO();
                BeanUtils.copyProperties(list.get(i), dishVO);
                dishVO.setFlavors(flavorMapper.byId(list.get(i).getId()));
                dishVOS.add(dishVO);
            }
            return dishVOS;
        }
        return null;
    }

    @Override
    public DishOverViewVO overviewDishs() {
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        dishOverViewVO.setSold(dishMapper.byStatus(1));
        dishOverViewVO.setDiscontinued(dishMapper.byStatus(0));
        return dishOverViewVO;
    }
}
